//
//  HHSettingViewController.m
//  HHBroker
//
//  Created by He Wei on 9/22/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHSettingViewController.h"
//#import "HHShareActionSheet.h"
//#import "ShareAppDelegate.h"
#import "HHSettingInfoModel.h"
#import "HHSettingInfoCell.h"
//#import <objc/message.h>
#import "HHBasicView.h"         //?




#define kAlertTagLogout 1000
#define KAlertTagChangePaYMent 1001

@interface HHSettingViewController () <UINavigationControllerDelegate>

//@property (nonatomic, strong) ShareAppDelegate *shareDelegate;
@end

@implementation HHSettingViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self prepareData];
}

#pragma mark -
#pragma mark - Function
- (void)logout
{
    [JiaYuanShareData deleteCurrentAccount];
    //NSDictionary *dic = @{KSignStatusKey:@(0)};
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"logout"
                                                        object:nil];

}

- (void)clearCache
{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    if ((0)) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_success"]];
        hub.mode = MBProgressHUDModeCustomView;
        hub.customView = imageView;
        hub.labelText = NSLocalizedString(@"清除成功", nil);
        hub.completionBlock = ^ {
            if (self.index) {
                [self.tableView deselectRowAtIndexPath:self.index animated:YES];
                [self.tableView reloadData];
            }
        };
        [hub hide:YES afterDelay:2];
        
    } else {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = NSLocalizedString(@"还没有缓存", nil);
        hub.completionBlock = ^ {
            if (self.index) {
                [self.tableView deselectRowAtIndexPath:self.index animated:YES];
            }
        
        };
        [hub hide:YES afterDelay:2];
    }

}

- (void)changePayMentCard
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确认修改银行卡" message:[NSString stringWithFormat:@"我们将发送短信验证码到您的帐户绑定手机上"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好", nil];
    alert.tag = KAlertTagChangePaYMent;
    [alert show];
}

- (void)toggleToChangePayMentCard
{
    __block MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hub.mode = MBProgressHUDModeIndeterminate;
    //网络请求
    //加入加密字段
    NSString *urlString = [NSString stringWithFormat:@"%@", KUrlCBindCardGetCode];
    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {
            // sent success
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_success"]];
            hub.mode = MBProgressHUDModeCustomView;
            hub.customView =imageView;
            hub.labelText = NSLocalizedString(@"发送成功，请查看手机短信", nil);;
            hub.completionBlock = ^ {
                // JYAddBankCardViewController *addBank = [[JYAddBankCardViewController alloc] init];
                //
                //
            
            
            };
            [hub hide:YES afterDelay:1];
        } else {
            hub.mode = MBProgressHUDModeText;
            hub.labelText = [response objectForKey:@"msg"];
            [hub hide:YES afterDelay:2];
        
        }
    
    } failure:^(NSError *error) {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"网络有点问题";
        [hub hide:YES afterDelay:2];
    } isLogin:YES];
}

//- (void)share
//{
//    HHShareActionSheet *share = [HHShareActionSheet shareActionSheetWithDeletate:self.shareDelegate];
//    [share show];
//    
//
//}
//
#pragma mark -Delegate

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseCell = @"reuseCell";
    if (indexPath.section == 2) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseCell];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = UIColorFromRGB(0xff6a6a);
        cell.textLabel.text = @"退出登录";
        cell.selectedBackgroundView = [HHBasicView tableViewCellSelectionBackgroundView];
        return cell;
    }
    HHSettingInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCell];
    if (cell == nil) {
        cell = [[HHSettingInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseCell];
    }
    cell.model = self.dataArray[indexPath.section][indexPath.row];
    return cell;
}


#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.index = indexPath;
    HHSettingInfoModel *model = self.dataArray[indexPath.section][indexPath.row];
    Class class = NSClassFromString(model.action);
    if ([class isSubclassOfClass:[UIViewController class]]) {
        HHBaseViewController *viewController = [[class alloc] init];
        viewController.title = model.title;
        viewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:viewController animated:YES];
        return;
    }
    SEL action = NSSelectorFromString(model.action);
    if ([self respondsToSelector:action]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self performSelector:action];
#pragma clang diagnostic pop
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        return;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
    }
    if (alertView.tag == kAlertTagLogout) {
        if (buttonIndex == 1) {
            [self logout];
        }
    }
    
    if (alertView.tag == KAlertTagChangePaYMent) {
        if (buttonIndex == 1) {
            [self toggleToChangePayMentCard];
        }
    }
}

#pragma mark - HHAddBankCardViewControllerDelegate









#pragma mark - Event
- (void)signOutButtonClick
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"你确定要退出吗?"
                                                        message:nil
                                                       delegate:self
                                              cancelButtonTitle:NSLocalizedString(@"取消", @"")
                                              otherButtonTitles:NSLocalizedString(@"退出账号", @""), nil];
    alertView.tag = kAlertTagLogout;
    [alertView show];
}

#pragma mark - Setter && Getter
- (void)prepareData
{
    //self.shareDelegate = [[ShareAppDelegate alloc] init];
    //self.shareDelegate.destinationVC = self;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"tab_setting" ofType:@"plist"];
    NSArray *dataArray = [NSArray arrayWithContentsOfFile:path];
    for (NSArray *array in dataArray) {
        NSMutableArray *sectionArray = [[NSMutableArray alloc] initWithCapacity:3];
        for (NSDictionary *dic in array) {
            HHSettingInfoModel *model = [[HHSettingInfoModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [sectionArray addObject:model];
        }
        [self.dataArray addObject:sectionArray];
    }

}


@end
